package com.jixiantubu.yueban;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnMapClickListener;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.example.jixiantubu.R;
import com.jixiantubu.bean.Ac_GeoCoder_Bean;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

public class YueBan_Map extends Activity {
	private Button geocode;
	private MapView mMapView = null;
	private BaiduMap mBaiduMap;
	private double latitude;
	private double longitude;
	private LatLng latLng;
	private String address;
	private GeoCoder mSearch;
	private LocationClient mLocClient;
	private BitmapDescriptor bitmap;
	private Ac_GeoCoder_Bean aBean;
	MyLocationListenner myListener = new MyLocationListenner();// 监听当前位置事件

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Window window = this.getWindow();
		window.requestFeature(window.FEATURE_NO_TITLE);
		setContentView(R.layout.ac_map);
		// 获取地图控件引用
		mMapView = (MapView) findViewById(R.id.bmapView);
		Toast.makeText(YueBan_Map.this, "请单击屏幕选择地点", Toast.LENGTH_LONG).show();
		initViews();
		initData();
	}

	private void initData() {
		mBaiduMap = mMapView.getMap();
		// 普通地图
		mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
		// 开始监听定位事件
		// 开启定位图层
		mBaiduMap.setMyLocationEnabled(true);
		// 定位初始化
		mLocClient = new LocationClient(this);
		mLocClient.registerLocationListener(myListener);
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);// 打开gps
		option.setCoorType("bd09ll"); // 设置坐标类型
		option.setScanSpan(1000);
		mLocClient.setLocOption(option);
		mLocClient.start();

		// 构建Marker图标
		bitmap = BitmapDescriptorFactory.fromResource(R.drawable.icon_gcoding);
		mBaiduMap.setOnMapClickListener(new OnMapClickListener() {
			@Override
			public void onMapClick(LatLng point) {
				// 在此处理点击事件
				// 构建MarkerOption，用于在地图上添加Marker
				OverlayOptions option = new MarkerOptions().position(point)
						.icon(bitmap);
				// 在地图上添加Marker，并显示
				mBaiduMap.clear();
				mBaiduMap.addOverlay(option);
				// 获取经纬度
				latitude = point.latitude;
				longitude = point.longitude;
				latLng = new LatLng(latitude, longitude);
				mSearch = GeoCoder.newInstance();
				mSearch.reverseGeoCode(new ReverseGeoCodeOption()
						.location(latLng));
				mSearch.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
					@Override
					public void onGetGeoCodeResult(GeoCodeResult result) {
						if (result == null
								|| result.error != SearchResult.ERRORNO.NO_ERROR) {
							// 没有检索到结果
						}
						// 获取地理编码结果

					}

					@Override
					public void onGetReverseGeoCodeResult(
							ReverseGeoCodeResult result) {
						if (result == null
								|| result.error != SearchResult.ERRORNO.NO_ERROR) {
							// 没有找到检索结果
						}
						// 获取反向地理编码结果
						address = result.getAddress();
						aBean = new Ac_GeoCoder_Bean(latitude, longitude,
								address);
						Toast.makeText(
								YueBan_Map.this,
								"地名：" + address + "\n" + "经度是：" + longitude
										+ "\n" + "纬度是：" + latitude,
								Toast.LENGTH_LONG).show();
					}
				});

			}

			@Override
			public boolean onMapPoiClick(MapPoi poi) {
				// 在此处理底图标注点击事件
				return false;
			}
		});
	}

	private void initViews() {
		geocode = (Button) findViewById(R.id.geocode);
		geocode.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {				
					Intent intent = new Intent(YueBan_Map.this,
							YueBan_MuLu_FaQi.class);
					intent.putExtra("abean", aBean);
					setResult(RESULT_OK, intent);
					finish();				
			}
		});
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// 在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
		mMapView.onDestroy();
		if (mSearch!=null) {
			mSearch.destroy();
		}
		
		
	}

	@Override
	protected void onResume() {
		super.onResume();
		// 在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
		mMapView.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
		// 在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
		mMapView.onPause();
	}

	/**
	 * 定位SDK监听函数
	 */
	public class MyLocationListenner implements BDLocationListener {

		private boolean isFirstLoc = true;

		@Override
		public void onReceiveLocation(BDLocation location) {
			// map view 销毁后不在处理新接收的位置
			if (location == null || mMapView == null)
				return;
			MyLocationData locData = new MyLocationData.Builder()
					.accuracy(location.getRadius())
					// 此处设置开发者获取到的方向信息，顺时针0-360
					.direction(100).latitude(location.getLatitude())
					.longitude(location.getLongitude()).build();
			mBaiduMap.setMyLocationData(locData);
			if (isFirstLoc) {
				isFirstLoc = false;
				LatLng ll = new LatLng(location.getLatitude(),
						location.getLongitude());
				MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
				mBaiduMap.animateMapStatus(u);
			}
		}

		public void onReceivePoi(BDLocation poiLocation) {
		}
	}

}
